package com.powerunion.datacollection.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import com.powerunion.datacollection.common.SystemException;
import com.powerunion.datacollection.dao.ColumnDao;
import com.powerunion.datacollection.dao.DaoFactory;
import com.powerunion.datacollection.model.Column;
import com.powerunion.datacollection.model.SubTblColumn;
import com.powerunion.datacollection.model.jsonObject.JSONDataBean;

import com.powerunion.util.log.LogHelper;

public class ColumnManager {
	private static Logger logger = LogHelper.getInstance().getLogger(ColumnManager.class);
	private ColumnDao dao = null;
	public ColumnManager() {
		super();
		this.dao = (ColumnDao)DaoFactory.getInstance().getDao(ColumnDao.class);
	}
	
	public boolean save(String saveStr,String formId) throws Exception{
		//参数为空
		if(StringUtils.isEmpty(saveStr)) {
			String[] args = { "saveStr" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter saveStr NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		//参数为空
		if(StringUtils.isEmpty(formId)) {
			String[] args = { "formId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter formId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		//先删除原有的信息
		this.dao.delete(formId);
		
		Map columnMap = new HashMap();
		columnMap.put("data", Column.class);
		JSONDataBean cl = (JSONDataBean)JSONObject.toBean(JSONObject.fromObject(saveStr), JSONDataBean.class,columnMap);

		return this.dao.save(cl.getData(),formId);
	}
	
	public boolean save4maintblColumn(String saveStr,String formId) throws Exception{
		//参数为空
		if(StringUtils.isEmpty(saveStr)) {
			String[] args = { "saveStr" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter saveStr NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		//参数为空
		if(StringUtils.isEmpty(formId)) {
			String[] args = { "formId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter formId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		
		Map columnMap = new HashMap();
		columnMap.put("data", SubTblColumn.class);
		JSONDataBean cl = (JSONDataBean)JSONObject.toBean(JSONObject.fromObject(saveStr), JSONDataBean.class,columnMap);
			
		List list = cl.getData();
		boolean flag = false;
		if(!list.isEmpty()){
			SubTblColumn stc = (SubTblColumn)cl.getData().get(0);
			flag = this.dao.save(stc, formId);
		}

		return flag;
	}
	
	public String getColumns(String formId) throws Exception {
		List columns = this.dao.getColumns(formId);
		JSONArray jsonArr = JSONArray.fromObject(columns);
		String result = "{results:"+jsonArr+"}";
		return result;
	}
	
	public int getColumnNumById(String formId) throws Exception{
		//参数为空
		if(StringUtils.isEmpty(formId)) {
			String[] args = { "formId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter formId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		return this.dao.getColumnNumById(formId);
	}
	
	public List getColumnsByFormId(String formId) throws Exception {
		//参数为空
		if(StringUtils.isEmpty(formId)) {
			String[] args = { "formId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter formId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		return this.dao.getColumns(formId);
	}
	
	public JSONArray getSubFormColumns(String formId) throws Exception {
		//参数为空
		if(StringUtils.isEmpty(formId)) {
			String[] args = { "formId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter formId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		List subColumns = this.dao.getSubFormColumns(formId);
		JSONArray jsonArray = JSONArray.fromObject(subColumns);
		return jsonArray;
	}
	
//	public void getGroupHeader(String formId) throws Exception {
//		List list = this.dao.getSubFormColumns(formId);
//		List groups = new ArrayList();
//		for(Iterator iter = list.iterator();iter.hasNext();){
//			
//		}
//	}
	
}
